package org.test.client.ui.component.paging;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.test.client.ui.util.VFlowLayout;

/**
 * 分页条
 * @author Guodeqi
 * 
 * setCurrentPage() 方法里面执行 页面切换时 需要的操作
 *
 */
public class PagingBar extends JPanel{
	
	public static int minBtnHeight = 10;
	public static int btnHeight = 40;
	public static int btnWidth = 35;
	
	int nowPage = 0;
	int totlePage = 0;
	//总共可以显示的 按钮个数 不算 上一页 下一页单数
	int maxBtnCount = 7;
	int centerBtnCount = maxBtnCount - 2;
	
	Vector<PagingBtn> btnList = null;
	PreAndNextBtn prevBtn = null;
	PreAndNextBtn nextBtn = null;
	ButtonGroup btnGroup = null;
	
	JLabel label1 = null;
	JLabel label2 = null;
	
	public int vgap = 2;
	
	public PagingBar(int nowPage, int totlePage) {
		this.nowPage = nowPage;
		this.totlePage = totlePage;
		FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER,0,vgap);
		this.setLayout(flowLayout);
		btnGroup = new ButtonGroup();
//		this.setBackground(Color.white);
		
		prevBtn = new PreAndNextBtn(true, "Prev");
		prevBtn.setParentBar(this);
		nextBtn = new PreAndNextBtn(false, "Next");
		nextBtn.setParentBar(this);
		label1 = createOmitLabel();
		label2 = createOmitLabel();
		
		btnList = new Vector<PagingBtn>();
		btnGroup = new ButtonGroup();
		
		if(totlePage <= maxBtnCount ){
			for (int i = 0; i < totlePage; i++) {
				PagingBtn tmp = new PagingBtn(i +1);
				btnList.add(tmp);
				btnGroup.add(tmp);
			}
			doBtnAdd(false,false,null);
		}else{
			for (int i = 0; i < maxBtnCount; i++) {
				PagingBtn tmp = new PagingBtn(i + i);
				btnList.add(tmp);
				btnGroup.add(tmp);
			}
		}
//		this.setBackground(Color.yellow);
		doInit();
	}
	
	
	public void doInit(){
		//初始化
		
		if(totlePage > maxBtnCount ){
			int[] nums = new int[maxBtnCount];
			
			boolean startLabel = false;
			boolean endLabel = false;
			int tmp =centerBtnCount/2;
			if( nowPage-tmp-2 > 0 ){
				startLabel = true;
			}
			if(nowPage+tmp +2 <= totlePage){
				endLabel = true;
			}
			
			//设置页码
			nums[0] = 1;
			nums[maxBtnCount-1] = totlePage;
			for(int i=1 ; i<maxBtnCount-1 ;i++){
				if(startLabel && endLabel){
					nums[i] = nowPage - tmp + i-1 ;
				}else if(!startLabel){
					nums[i] = nums[0]  + i ;
				}else if(!endLabel){
					nums[i] = nums[maxBtnCount-1] - (centerBtnCount-i)-1 ;
				}
			}
			
			doBtnAdd(startLabel,endLabel,nums);
		}
		
		
	}
	
//	public void setPagingNum(int[] nums){
//		int size = btnList.size();
//		for(int i=0; i<nums.length ;i++){
//			btnList.get(i).setPagingNum(nums[i]);
//		}
//	}
	
	public void doBtnAdd(boolean startLabel,boolean endLabel,int[] nums){
		this.removeAll();
		this.add(prevBtn);
		
		int size = btnList.size();
		for(int i=0; i<size ;i++){
			
			PagingBtn pagingBtn = btnList.get(i);
			pagingBtn.needRightBorderLine = false;
			this.add(pagingBtn);
			
			if(startLabel && i==0){
				this.add(label1);
				pagingBtn.needRightBorderLine = true;
			}
			if(endLabel && i==size-2){
				this.add(label2);
				pagingBtn.needRightBorderLine = true;
			}
			if(nums != null){
				pagingBtn.setPagingNum(nums[i]);
			}
			
		}
		this.add(nextBtn);
		setSelectedPage(nowPage);
	}
	
	public void setSelectedPage(int page){
		int size = btnList.size();
		for(int i=0; i<size ;i++){
			PagingBtn tmp = btnList.get(i);
			int currrentPageNum = tmp.getCurrrentPageNum();
			if(page == currrentPageNum){
				tmp.setSelected(true);
			}
		}
	}
	
	public void setCurrentPage(int page){
		if(page < 1 || page > totlePage){
			return;
		}
		int back = nowPage;
		nowPage = page;
		doInit();
		this.doLayout();
		this.repaint();
		
		//do page changeAction
//		System.out.println("页面由"+back+"变换到 "+page);
	}
	
	public JLabel createOmitLabel(){
		JLabel label = new JLabel(" ... ");
		label.setForeground(Color.black);
		return label;
	}
	
	
	@Override
	public Component add(Component c) {
		
		if(c instanceof PagingBtn){
			PagingBtn b = (PagingBtn) c;
			b.setParentBar(this);
			btnGroup.add(b);
		}
		return super.add(c);
	}
	
	public int getCurrentPage() {
		return nowPage;
	}


	public static void main(String[] args) {
		JFrame frame = new JFrame();
		Container contentPane = frame.getContentPane();
		JPanel panel = new JPanel(new VFlowLayout());
		
		for(int i=1;i<11;i++){
			PagingBar bar = new PagingBar(i,100);
			bar.setPreferredSize(new Dimension(0,50));
			panel.add(bar);
		}
		
		contentPane.add(panel);
		
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		frame.setSize(700, 600);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	@Override
	public void setBounds(int i, int j, int k, int l) {
		reSizePagnigBtn(k,l);
		super.setBounds(i, j, k, l);
	}
	
	@Override
	public void setSize(int i, int j) {
		reSizePagnigBtn(i,j);
		super.setSize(i, j);
	}
	
	@Override
	public void setPreferredSize(Dimension d) {
		reSizePagnigBtn(d.width,d.height);
		super.setPreferredSize(d);
	}
	
	//设置bar的 大小时候 会 将btn 的大小重置
	public void reSizePagnigBtn(int w,int h){
		h = h- vgap*2;
		if(h != 0 && h != btnHeight){
			if(h < minBtnHeight){
				h = minBtnHeight;
			}
			prevBtn.setCurrentH(h);
			nextBtn.setCurrentH(h);
			for(PagingBtn  tmpBtn: btnList){
				tmpBtn.setCurrentH(h);
			}
		}
	}
}
